import gameModel from "./gameModel";
import { UIMgr } from "../manager/viewMgr";
import { ZYEventMgr } from "../manager/YLEventMgr";
import { ZYNetMgr } from "../server/ZYNetMgr";
import { ADMgr } from "../wxComponent/ADManager";
import { ZYSDK } from "../../../ZYSDK";

/**
 * 事件控制中心
 * @author: xj.huang
 * @date: 2021/08/12 09:36:51
 */
class _SdkEventModel {

    constructor() {

    }
    private static _instance: _SdkEventModel;
    public static get instance(): _SdkEventModel {
        if (!this._instance) {
            this._instance = new _SdkEventModel();
        }
        return this._instance;
    }
    initEventMode() {
        this.onAddEvent()
    }
    private onAddEvent() {
        ZYEventMgr.on(GameEvent.GAME2SDK_OVER_SUCCESS, this, this.onGameSuccess)
        ZYEventMgr.on(GameEvent.GAME2SDK_OVER_FAIL, this, this.onGameFail)
    }
    /**游戏成功 */
    private async onGameSuccess() {
        this.onUpUserdata();
        this.onResetGame(true)
    }
    /**更新用户数据 */
    private onUpUserdata() {
        if (gameModel.loginData.user) {
            gameModel.userData.currLevel += 1
            gameModel.currLevel = gameModel.userData.currLevel;
            let province = gameModel.loginData.user.ip_province;
            let city = gameModel.loginData.user.ip_city;
            ZYNetMgr.updateUserExtData({ ext_data: JSON.stringify(gameModel.userData), province: province, city: city })
        }
    }
    /**游戏失败 */
    private async onGameFail() {
        this.onResetGame(false)
    }
    /**重新开始游戏 */
    public async onResetGame(isScuees) {
        await UIMgr.asyncExportOpen(ZYSDK.gameConfig.box_conf.end, "EVENT", async (res) => {
            await UIMgr.asyncOpen(ViewRes.VIEW_GAME_END, { isScuees: isScuees });
        })
        Laya.timer.once(200, this, () => {
            ZYEventMgr.emit(GameEvent.SDK2GAME_REPLAY_GAME, [gameModel.currLevel])
            ZYEventMgr.emit(GameEvent.SDK2GAME_CURR_LEVEN, [gameModel.currLevel])
        })
    }
    /**开始游戏 */
    public onPlayGame() {
        ZYEventMgr.emit(GameEvent.SDK2GAME_PLAY_GAME, [gameModel.currLevel]);
        ZYEventMgr.emit(GameEvent.SDK2GAME_CURR_LEVEN, [gameModel.currLevel])
    }
}
export const SdkEventModel = _SdkEventModel.instance